//view js
var rootPath = getRootPath();
var isRightPhoneNum = false;
$(function(){
	$("#user,#message").click(chgLabel);
	$.ajax({
		url: rootPath + '/user/queryUserself',
		type: 'GET',
		contentType: 'application/json;charset=UTF-8',
		success: function(data) {
			if(data.returnCode == '0000') {
				var user = data.returnObject;
//				$("#headimgurl").prop("src", rootPath + "/file/viewimg?fileName=" + user.headimgurl + "&type=1");
				if(user != null && user.headimgurl != null && user.headimgurl != '') {
					$("#headimgurl").attr("src",rootPath + "/file/viewimg?fileName=" + (user.headimgurl) + "&type=1");
				} else {
					$("#headimgurl").attr("src","../../images/moren.png");
				}
					//页面"昵称"对应数据库uname,数据库nickName对应"微信用户"昵称
				$("#nickName").html(user.uname);
				$("#cts").html(user.cts);
			 
				 
			}
		}
	});
	
	$("#message_code_img,#message_next").click(refreshCheckCode);
	refreshMessageCheckCode();
	
	$("#code_img,#next").click(refreshCheckCode);
	
	$("#username_input,#password_input,#checkcode_text").bind(
			'input propertychange', chgBtnState);
 
	
	// 短信登录
	$("#phone_input,#message_checkcode_text,#message_pic_checkcode_text").bind('input propertychange',
			chgBtnState);

	$("#checkcode_get_code").css({
		"color" : "#bbbbbb",
		"cursor" : "default"
	});

	// 限制只能输入数字
	$("#phone_input").keyup(function() {
		$(this).val($(this).val().replace(/[^\d]/g, ''));
	});
	//1.手机号码重新输入（获取焦点），去除所有错误控制信息
	$("#phone_input").focus(function() {
		hideMsg();
		$("#message_checkcode_text").val("");
		$("#message_login_click").removeClass("active"); 
		isRightPhoneNum = false;
	});
	// 2.手机号码判定是否合法（移除焦点），如果合法，则可以获取验证码/否则显示错误信息。
	$("#phone_input,#message_pic_checkcode_text").blur(function() {
		if($.trim($(this).val())==""){
			return;
		}
		if (!checkPhone($("#phone_input").val())) {
			showPhoneErrMsg("请填写有效的11位手机号码");
			$("#checkcode_get_code").css({
				"color" : "#bbbbbb",
				"cursor" : "default"
			});
			return;
		}
		
		if(isNull($("#message_pic_checkcode_text").val()) || $.trim($("#message_pic_checkcode_text").val()).length != 4) {
			isRightPhoneNum = false;
			$("#checkcode_get_code").css({
				"color" : "#bbbbbb",
				"cursor" : "default"
			});
			return;
		}

		$.ajax({
			type : "get",
			url : rootPath + "/userFront/accountMerge/checkPhone",
			async : true,
			dataType : "json",
			data : {
				"phone" : $.trim($("#phone_input").val())
			},
			success : function(data) {
				if ("0000" == data.returnCode) {
					isRightPhoneNum = true;
					hideMsg();
					$("#checkcode_get_code").css({
						"color" : "#B4282D",
						"cursor" : "pointer"
					});
				} else {
					isRightPhoneNum = false;
					showPhoneErrMsg(data.returnMessage);
					$("#checkcode_get_code").css({
						"color" : "#bbbbbb",
						"cursor" : "default"
					});
				}
			}
		});
	});

	// 4.发送验证码之后，显示倒计时60秒。
	$("#checkcode_get_code").click(getCheckCode);
});

function refreshMessageCheckCode(){
	$("#message_code_img").attr("src",'/account/captcha.jpg?tm='+Math.random());
}

function refreshCheckCode(){
	$("#code_img").attr("src",'/account/captcha.jpg?tm='+Math.random());
}
//
function chgLabel(e) {
	$("#message,#user").removeClass("select");
	var c = $(e.target);

	c.addClass("select");
	id = c.attr("id");

	if (id == "message") {
		$("#label_line").removeClass("message", 300);
		$("#user_layer").css("display", "none");
		$("#message_layer").css("display", "block");
		refreshMessageCheckCode();
	} else {
		$("#label_line").addClass("message", 300);
		$("#user_layer").css("display", "block");
		$("#message_layer").css("display", "none");
		refreshCheckCode();
	}
};




//获得验证码接口
function getCheckCode() {
	if (!isRightPhoneNum) {
		return;
	}
	if ($.isEmptyObject($("#phone_input").val())) {
		showPhoneErrMsg("请输入手机号。");
		return;
	}
	// 倒计时中不能发送
	var countdown = $("#checkcode_get_code").attr("countdown");
	if (!$.isEmptyObject(countdown) && countdown > 0) {
		return;
	}

	showPhoneErrMsg("正在发送,请稍等");
	setTime($("#checkcode_get_code"));
	$.ajax({
		type : "get",
		url : rootPath + "/userFront/sendCodeSmsValidate",
		data : {
			"phone" : $("#phone_input").val(),
			"flag" : 9,
			"checkCode" : $("#message_pic_checkcode_text").val()
		},
		async : true,
		dataType : "json",
		success : function(data) {
			if ("0000" == data.returnCode) {
				showPhoneErrMsg("发送成功");
			} else {
				showPhoneErrMsg(data.returnMessage);
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			showPhoneErrMsg("发送验证码失败，请检查您的网络。");
		}
	});
}

// 验证码倒计时
function setTime(obj) {
	var countdown = obj.attr("countdown");
	if ($.isEmptyObject(countdown) || countdown == 0) {
		countdown = 60;
	}
	var interval = setInterval(function() {
		countdown--;
		obj.attr("countdown", countdown);
		if (countdown == 0) {
			obj.css({
				"color" : "#B4282D",
				"cursor" : "pointer"
			});
			obj.attr('disabled', false);
			obj.text("获取验证码");
			window.clearInterval(interval);
		} else {
			obj.css({
				"color" : "#bbbbbb",
				"cursor" : "default"
			});
			obj.attr('disabled', true);
			obj.text("重新发送(" + countdown + ")");
			// if(countdown == 59) {
			// showPhoneErrMsg("发送成功");
			// }
		}
	}, 1000);
};
/**
 * 隐藏错误框
 * 
 * @returns
 */
function hideMsg() {
	$("#phone_msg").text("");
	$("#user_msg").text("");
	$(".msg").hide();
};
function showPhoneErrMsg(msg) {
	if (!$.isEmptyObject(msg)) {
		$("#phone_msg").text(msg);
		$("#phone .msg").show();
	}
};
function showUserErrMsg(msg) {
	if (!$.isEmptyObject(msg)) {
		$("#user_msg").text(msg);
		$("#username .msg").show();
	}
};
function chgBtnState() {
	 // 是否将按钮变成可点击
	if ($.trim($("#username_input").val()) == '' || $.trim($("#password_input").val()) == ''
			|| $.trim($("#checkcode_text").val()) == ''
			|| $("#checkcode_text").val().length != 4) {
		$("#user_login_click").removeClass("active");
	} else {
		$("#user_login_click").addClass("active");
	}
	console.log("mm",$("#message_checkcode_text").val())
	if ($.trim($("#phone_input").val()) == '' || !checkPhone($("#phone_input").val())
			|| $.trim($("#message_pic_checkcode_text").val()) == ''
			|| $.trim($("#message_pic_checkcode_text").val()).length != 4
			|| $.trim($("#message_checkcode_text").val()) == ''
				|| $.trim($("#message_checkcode_text").val()).length != 4
			|| !isRightPhoneNum) {
		$("#message_login_click").removeClass("active");
	} else {
		$("#message_login_click").addClass("active");
	}

	// 修改用户名输入框，删除错误提示消息
	if ($.isEmptyObject($("#username_input").val())) {
		$("#user_layer .msg").hide();
	}
};